package se.liu.oschi129.game.objects.blocks.breakable;

import java.util.List;
import java.util.ArrayList;

import se.liu.oschi129.animation.Animation;
import se.liu.oschi129.world.model.World;
import se.liu.oschi129.world.model.WorldGraphics;
import se.liu.oschi129.world.objects.WorldObject;
import se.liu.oschi129.world.objects.WorldStaticObject;

/**
 * This is an abstract class for breakable blocks.
 * 
 * @author oschi129
 */
public abstract class ObjectBreakableBlock extends WorldStaticObject {

	// This variable contains information of which phase the object is in (0 - 2 = moving up, 3 - 5 moving down)
	private int step;

	// The translation among the y-axis
	private double dy;

	// Whatever the block is hit or not
	private boolean hit;
	
	// The parts content of the block
	private final List<WorldObject> parts = new ArrayList<WorldObject>();
	
	/**
	 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	 * - - - - - - - - - - - - Constructors - - - - - - - - - - - 
	 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	 */
	
	public ObjectBreakableBlock(double x, double y, double width, double height) {
		super(x, y, width, height);
		setType(Type.SOLID);
	}
	
	public ObjectBreakableBlock(Animation animation, boolean visible, double x, double y, double width, double height) {
		this(x, y, width, height);
		setAnimation(animation);
		setVisible(visible);
	}
	
	/**
	 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	 * - - - - - - - - - - - Public methods - - - - - - - - - - 
	 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	 */
	
	/** Adds a part the the block */
	public void addPart(WorldObject part) {
		parts.add(part);
	}
	
	/** Hits the block to destroy it */
	@Override public void performHitAction(World world) {
		if (!hit)
			hit = true;
	}
	
	@Override public void performDestructionAction(World world) {
		world.setPoints(world.getPoints() + 10);
		for (WorldObject part : parts) {
			world.addObjectSynchronized(part, getX() + getWidth()/2, getY() + getHeight()/2);
		}
	}
	
	@Override public void performStepAction(World world) {
		super.performStepAction(world);
		if (hit) {
			if (step == 2) {
				world.removeObjectSynchronized(this);
			} else if (step < 2)
				dy-=4; // Moving up
			step++;
		}
	}
	
	/** Overrides the drawing action - the image needs to be translated when hit */
	@Override public void performDrawingAction(WorldGraphics worldg) {
		worldg.drawAnimation(getX(), getY() + dy, getWidth(), getHeight(), getAnimation(), (int) getImageIndex());
	}
	
	
	
}
